package com.cbwl.eoas.common.file;

import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * <p>[描述信息：读取异常描述的properties文件]</p>
 *
 * @author yuyunbo
 * @version 1.0 Created on 2016-4-12 下午02:32:47
 */
public class SysPropertiesUtil {

	private static Logger LOG = LoggerFactory.getLogger(SysPropertiesUtil.class);


	// properties 文件中的 value
	private static Map<String, String> map;

	static {
		// 读取properties文件
		fileToMap();
	}

	/**
	 * 根据java标准properties文件读取信息，并赋值为一个 HashMap<String,String>
	 * @param path
	 * @param map
	 * @return
	 * @throws Exception
	 */
	private final static void fileToMap() {
		if (map == null) {
			map = new HashMap<String, String>();
		}
		InputStreamReader is = null;
		try {
			
			is = new InputStreamReader(SysPropertiesUtil.class.getClassLoader().getResourceAsStream("properties/system.properties"), "UTF-8");
			// 从输入流中读取属性列表
			Properties props = new Properties();
			// 装载配置文件
			props.load(is);
			Set<Entry<Object, Object>> entrySet = props.entrySet();
			
			LOG.info("===="+String.valueOf(entrySet.size()));
			for (Entry<Object, Object> entry : entrySet) {
				if (!entry.getKey().toString().startsWith("#")) {
					map.put(((String) entry.getKey()).trim(), ((String) entry.getValue()).trim());
				}
			}
		} catch (Exception e) {
			LOG.error(ExceptionUtils.getStackTrace(e));
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (Exception e2) {
					LOG.error(ExceptionUtils.getStackTrace(e2));
				}
			}
		}
	}

	/**
	 * 
	 * <p>通过异常代码获取异常描述信息</p>
	 * 
	 * @param exCode
	 * @return
	 * @author: yuyunbo 
	 * @date: Created on 2016-4-12 下午02:35:27
	 */
	public static String getValue(String smcode) {
		return map.get(smcode);
	}

	public static void main(String[] args) {
		System.out.println(getValue("CBWL-000004"));
	}
}
